import { AssetAllotManageAPI } from '../common/type';
import { Button, Tooltip, message } from 'antd';
import { downloadTemplate, getColorTextLoadMap, getFileNameNotType, getTenantID, getUserID, removeDateHmsStr, splitFileUrl, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, EyeOutlined, CloseCircleOutlined, CheckCircleOutlined, DeleteOutlined, RollbackOutlined } from '@ant-design/icons';
import { stateOfRendering } from '../common';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal } from '../../Acommon';
import { history } from 'umi';
import { getKeyRouter } from '@/utils';
import MyIcon from '@/components/MyIcon';
import { ILoadMapTypeAll } from '@/common';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: AssetAllotManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  // 没有审批状态 并且 申请状态在起草  显示可以编辑
  const editVisible: boolean =
    curRes?.['/assetApplyTransfer/edit'] && !record?.checkState && record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_TRANSFER_STATUS}1000`;
  // 没有审批状态或者审批被退回 并且 申请状态在起草  显示可以提交
  const submitVisible: boolean =
    (!record?.checkState && record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_TRANSFER_STATUS}1000`) ||
    (record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
      record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_TRANSFER_STATUS}1002`);

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: editVisible,
    },
    // {
    //   icon: <CheckCircleOutlined />,
    //   btnName: '提交',
    //   clickFnc: operateFuns,
    //   clickType: 'onSubmit',
    //   isVisible: false, // submitVisible
    // },
    {
      icon: <DeleteOutlined style={{}} />,
      btnName: <span style={{}}>删除</span>,
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible: submitVisible,
    },
    {
      icon: <RollbackOutlined style={{}} />,
      btnName: <span style={{}}>撤销</span>,
      clickFnc: operateFuns,
      clickType: 'onRevoke',
      isVisible:
        record?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_TRANSFER_STATUS}1001` &&
        record?.createrID == getUserID(),
    },
    // {
    //   icon: <EditOutlined />,
    //   btnName: 'OA',
    //   clickFnc: operateFuns,
    //   clickType: 'onOA',
    //   // 没有审批状态 并且 申请状态在起草  显示可以编辑
    //   isVisible: true,
    // },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

// Table表格的配置
const getTableColumns = (curRes: Record<string, any>, operateFuns: AssetAllotManageAPI.IMaterialTableActionType) => {
  const { onView } = operateFuns;
  return [
    {
      title: '申请部门/单位',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      fixed: 'left',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.applyInfo > b?.applyInfo ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '事项名称',
      dataIndex: 'applyTitle',
      key: 'applyTitle',
      fixed: 'left',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.applyInfo > b?.applyInfo ? 1 : -1),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <a className={pageStyles['over-flow-hidden']} onClick={() => onView(record)}>
              {text || '--'}
            </a>
          </Tooltip>
        );
      },
    },
    {
      title: '申请人',
      dataIndex: 'createrName',
      key: 'createrName',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.createrName > b?.createrName ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: true,
      width: 150,
      unDrag: true,
      sorter: (a: any, b: any) => (a?.createTime > b?.createTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    // {
    //   title: '调拨说明',
    //   dataIndex: 'applyInfo',
    //   key: 'applyInfo',
    //   fixed: 'left',
    //   width: 150,
    //   unDrag: true,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.applyInfo > b?.applyInfo ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      width: 150,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.stateName > b?.stateName ? 1 : -1),
      render: (text: number) => {
        let colorText = getColorTextLoadMap(text)?.colorText;
        let color = getColorTextLoadMap(text)?.color;
        return (
          <Tooltip>
            <MyIcon type="icon-dian" style={{ color: color }} />
            <span style={{ color: color }}>{colorText ? colorText : '--'}</span>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '所属仓库',
    //   dataIndex: 'assetStoreName',
    //   key: 'assetStoreName',
    //   width: 150,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a.assetStoreName > b.assetStoreName ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    // {
    //   title: '审核状态',
    //   dataIndex: 'checkStateName',
    //   key: 'checkStateName',
    //   width: 150,
    //   unDrag: true,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.checkStateName > b?.checkStateName ? 1 : -1),
    //   render: (text: string) => {
    //     let colorText = getColorTextLoadMap(text)?.colorText;
    //     let color = getColorTextLoadMap(text)?.color;
    //     return (
    //       <Tooltip>
    //         <MyIcon type="icon-dian" style={{ color: color }} />
    //         <span style={{ color: color }}>{colorText ? colorText : '--'}</span>
    //       </Tooltip>
    //     );
    //   },
    // },
    // {
    //   title: '原仓库',
    //   dataIndex: 'originalStoreName',
    //   key: 'originalStoreName',
    //   width: 150,
    //   unDrag: true,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.originalStoreName > b?.originalStoreName ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    // {
    //   title: '现仓库',
    //   dataIndex: 'newStoreName',
    //   key: 'newStoreName',
    //   width: 150,
    //   unDrag: true,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.newStoreName > b?.newStoreName ? 1 : -1),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    // {
    //   title: '调拨申请单',
    //   dataIndex: 'applyFile',
    //   key: 'applyFile',
    //   width: 150,
    //   unDrag: true,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.applyFile > b?.applyFile ? 1 : -1),
    //   render: (text: string, record: any) => {
    //     return (
    //       <div
    //         style={{
    //           height: '100%',
    //           display: 'flex',
    //           alignItems: 'center',
    //           marginLeft: '8px',
    //         }}
    //       >
    //         {record?.['applyFile']
    //           ? splitFileUrl(record?.['applyFile'])?.map((e, i) => {
    //               return (
    //                 <Tooltip title={e.attachName} key={new Date().getTime() * 25615265152 + e?.attachName}>
    //                   <a
    //                     className={pageStyles['over-flow-hidden']}
    //                     onClick={() => {
    //                       message.success('下载中，请稍后...');
    //                       downloadTemplate('/assetInfo/downloadFile', e?.attachName, e?.attachUrl, () => {});
    //                     }}
    //                   >
    //                     <MyIcon type="icon-file-text-fill" />
    //                     &nbsp;
    //                     {getFileNameNotType(e.attachName, 8)}
    //                   </a>
    //                 </Tooltip>
    //               );
    //             })
    //           : '--'}
    //       </div>
    //     );
    //   },
    // },
    // {
    //   title: '创建人单位',
    //   dataIndex: 'createCompanyName',
    //   key: 'createCompanyName',
    //   visible: false,
    //   width: 150,
    //   unDrag: false,
    //   sorter: (a: any, b: any) => (a?.createCompanyID > b?.createCompanyID ? 1 : -1),
    //   render: (text: string | null) => ColumnsRenderFun({ text }),
    // },
    // {
    //   title: '创建人岗位',
    //   dataIndex: 'createPositionName',
    //   key: 'createPositionName',
    //   visible: false,
    //   width: 150,
    //   unDrag: false,
    //   sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
    //   render: (text: string | null) => ColumnsRenderFun({ text }),
    // },
    // {
    //   title: '创建人',
    //   dataIndex: 'createrName',
    //   key: 'createrName',
    //   visible: false,
    //   width: 150,
    //   unDrag: false,
    //   sorter: (a: any, b: any) => (a?.createrName > b?.createrName ? 1 : -1),
    //   render: (text: string | null) => ColumnsRenderFun({ text }),
    // },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyTime > b?.modifyTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人单位',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyCompanyName > b?.modifyCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyerName > b?.modifyerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废类型', //数据字典
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTypeName > b?.cancelTypeName ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 180,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelReason > b?.cancelReason ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTime > b?.cancelTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '作废人单位',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelCompanyName > b?.cancelCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelDepartmentName > b?.cancelDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelPositionName > b?.cancelPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelerName > b?.cancelerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 180,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operateFuns, record)}</div>;
      },
    },
  ];
};

export { getTableColumns };
